﻿$(document).ready(function () {
    var cusid = getUrlVars()["c"];
    getCusData(cusid);

});

function getCusData(cusid) {
    if (cusid.length != 0) {
        var params = { 'cusid': cusid }
        var containerDIV = $('#addBrandLoadingGif');
        var opts = { 'isLoadingGif': true, 'container': containerDIV };
        MakeJAjaxCall("../../_services/getCusData.ashx", params, 'html', fnGetCusDataSuccess, '', '', opts);
    }
}
function fnGetCusDataSuccess(response) {
    if (response != 'n') {
        var data = $.parseJSON(response);
        if (data.length > 0) {
            for (var i = 0; i < data.length; i++) {
                $('#cusid').text(data[i].id);
                $('#txtCusName').val(data[i].name);
                $('#txtMobile').val(data[i].mobile);
                $('#txtAltPhone').val(data[i].landline);
                $('#txtAddress1').val(data[i].address);
                $('#txtLandmark').val(data[i].landmark);
                $('#txtCity').val(data[i].city);
                $('#txtState').val(data[i].state);
                $('#txtPostcode').val(data[i].postcode);
                $('#txtEmailID').val(data[i].email);
            }
        }
    }
}
function fnEditCancelClick() {
    var cusid = getUrlVars()["c"];
    window.location.href = "../../pages/customerdetails.aspx?cid=" + cusid;

}
function fnUpdateCustomerData() {
    var cusid = getUrlVars()["c"];
    var name = $('#txtCusName').val();
    var mobile = $('#txtMobile').val();
    var landline = $('#txtAltPhone').val();
    var address = $('#txtAddress1').val();
    var landmark = $('#txtLandmark').val();
    var city = $('#txtCity').val();
    var state = $('#txtState').val();
    var postcode = $('#txtPostcode').val();
    var email = $('#txtEmailID').val();
    if (name.length == 0) {
        $('#valFname').text("** Customer name cannot be blank");
        return false;
    }
    if (mobile.length == 0) {
        $('#valMobile').text("** Enter the customer mobile number");
        return false;
    }
    if (email.length == 0) {
        $('#valEmail').text("** Please enter the customer email");
        return false;
    }
    var params = { 'name': name, 'mobile': mobile, 'landline': landline, 'address': address, 'landmark': landmark, 'city': city, 'state': state, 'postcode': postcode, 'email': email,'cusid': cusid }
    var containerDIV = $('#addBrandLoadingGif');
    var opts = { 'isLoadingGif': true, 'container': containerDIV };
    MakeJAjaxCall("../../_services/updateCustomerData.ashx", params, 'html', fnUpdateCusDataSuccess, '', '', opts);
}
function fnUpdateCusDataSuccess(response) {
    if (response == "y") {
        var msg = 'Customer data updated successfully';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { fnEditCancelClick(); $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
    else {
        var msg = 'Error: Unable to update customer data';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}